package com.hdoit.pisces.web.other.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.pisces.logical.rel.model.Relationship;
import com.hdoit.pisces.logical.rel.model.Relative;
import com.hdoit.pisces.logical.rel.service.IRelationshipService;
import com.hdoit.pisces.logical.rel.service.IRelativeService;
import com.hdoit.pisces.logical.viewmodel.RelationshipSearch;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.logical.viewmodel.superbase.EmployeeSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.component.web.BaseController;

@Controller
@RequestMapping(value="/admin/")
public class MainnewController extends BaseController {
	
	@Override
	public String getRoutePath() {
		return "areas/admin/";
	}
	
	private IRelationshipService relationshipService;
	
	public IRelationshipService getRelationshipService(){
		return relationshipService;
	}
	
	@Resource
	public void setRelationshipService(IRelationshipService relationshipService){
		this.relationshipService=relationshipService;
	}
	
	private IRoleEmployeeService roleEmployeeService;
	
	public IRoleEmployeeService getRoleEmployeeService(){
		return roleEmployeeService;
	}
	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService){
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private IRoleService roleService;
	
	public IRoleService getRoleService(){
		return roleService;
	}
	
	@Resource
	public void setRoleService(IRoleService roleService){
		this.roleService = roleService;
	}
	
	private IRelativeService relativeService;

	
	public	IRelativeService getRelativeService() {
		return relativeService;
	}
	 
	
	@Resource
	public void setUserService(IRelativeService relativeService ){
		this.relativeService=relativeService;
			}
	
	
	private final String mainViewName="mainnew";
	
	@RequestMapping(value = mainViewName, method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model,RelationshipSearch relationshipSearch,EmployeeSearch search,PagerModel pagerModel,boolean ajaxPost) {
		String key = getLoginModel().getLoginId();
		System.out.println(getLoginModel().getLoginId());
		String userKey = roleService.queryModelByMark("3").getRoleId();//普通用户的roleid
		System.out.println(userKey);
		String managerKey = roleService.queryModelByMark("2").getRoleId();//普通管理员的roleid

		List<Relative> relatives= new ArrayList<Relative>();
		//System.out.println(relatives.size()+"11111111111111111111111111111111");
		List<Relationship> relationships=relationshipService.queryList();
		System.out.println(relationships.size()+"22222222222222222222222");
		
		//List<RoleEmployee>role_employees=roleEmployeeService.queryList(search);
		
		if(roleEmployeeService.queryRoleIdsByEmployeeId(key).get(0).equals(userKey)){
			//普通用户
			System.out.println("333333333333333333333333");
			//search.setSearchKey(key);
			Relationship tmprel=new Relationship();
			tmprel.setEmployeeId(key);
			System.out.println("444444444444444444444444");
			List<Relationship> relations=relationshipService.queryList(tmprel);
			System.out.println(relations.size()+"5555555555555555555555");
			for(int i=0;i<relations.size();i++){
				System.out.println();
				relatives.add(relativeService.queryModel(relations.get(i).getRelativeId()));

			}
			
		}else{
			//超级管理员或管理员
			relatives = relativeService.queryList();
		//	List<Relationship> relationships=relationshipService.queryList(relationshipSearch);
			pagerModel.setTotalAmount(relationshipService.queryCount(relationshipSearch));
		}
		model.addAttribute("relatives", relatives);
		Employee employee =new Employee();
		model.addAttribute("employee", employee);
		
		return view(mainViewName);
	}
	

	
	
}
	
